package models;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import play.mvc.*;
import play.libs.*;
import scala.concurrent.Await;
import scala.concurrent.duration.Duration;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.node.ArrayNode;

public class Player{

    public static int PLAYER_IN_GAME = 0;
    public static int PLAYER_OUT_OF_GAME = -1;
    public static int PLAYER_PLAYED_ALLIN = 1;
    
    private String name;
    private Game game;
    private int number;
    private int playersCoins;
     /**
   * Bet
   * 
   * @var bet Current Bet
   */
    private int bet = 0;
     /**
   * Status of player
   * 
   * @var status status of player
   */
    private int status = PLAYER_IN_GAME;
    private Hand PlayerHand;

   
    private boolean works;
    private boolean ready;
    private ServerConnection connection;
    public int voteForRejection;
    public boolean rightToVote;
     /**
   * Player.
   * 
   * @param number Player number
   * @param socket Player socket
   * @param moc Manager of Clients
   * @param startingCoins Coins at the start
   * @throws ClientException while serverConnection is unstable
   */
    public Player(int number, String name, int startingCoins){

       
        this.game = game;
        this.number = number;
        this.playersCoins = startingCoins;
        this.voteForRejection = 0;
        this.ready=false;
        this.rightToVote = false;
        this.works = true;
        this.name = name;
    }
    public void  setConnection(ServerConnection connection)
    {
			this.connection = connection;

    }
    public void startGame(Game game, Deck deck)
    {
        this.game = game;
        this.PlayerHand = new Hand(deck, number);

       // this.send("GameStart_");
        this.send("startCoins_" + playersCoins);
        this.send("startCards_" + PlayerHand.getString());
        this.connection.startGame();
    }
    
    public void stopGame()
    {
    	ready =false;
    	connection.stopGame();
    }
    public void setNumber(int n )
    {
    	this.number =n;
    }
    public String getNick() {
        return name;
    }
    
    public void setNick(String nick)
    {
    	this.name = nick;
    }
	/**Send data to client.*/
	public void send(String data)
	{
		connection.send(data);
	}
    public int getNumber() {
        return number;
    }
    public int getBet(){
        return bet;
    }
    public int getPlayersCoins() {
        return playersCoins;
    }
    public Hand getPlayerHand() {
        return PlayerHand;
    }
    public void exchangeCards(Deck deck, int cardSet){
        PlayerHand.changeCards(deck, number, cardSet);
    }
    public void setBet(int bet){
        this.bet = bet;
    }
    public void setStatus(int status){
        this.status = status;
    }
    public int getStatus(){
        return status;
    }
    public boolean isWorking(){
        return works;
    }
    public void setPlayersCoins(int coins) {
        this.playersCoins = coins;
    }

    public void displayHand(){
        this.send("Hand_" + PlayerHand.getString() );
    }
    public void displayBigBlind(int id, String bBlind){
        this.send("BigBlind_" + id + bBlind);
    }
    public void displaySmallBlind(int id,String sBlind){
        this.send("SmallBlind_" + id + sBlind);
    }
    public void displayCoins(String amount, int id){
        this.send("sCoins_"+ id + amount);
    }
    public void displayBet(String bet, int id){
        this.send("Bet_" + id + bet);
    }
    public void enableButtons(String buttons){
        this.send("Enable_"+buttons);
    }
    public void displayFold(int Id){
        this.send("dFold_"+ Id);
    }
    public void displayAllin(int Id){
        this.send("dAllin_"+ Id);
    }
    public void displayStake(String stake){
        this.send("Stake_" + stake);
    }
    public void displayRound(String round){
        this.send("Round_" + round);
    }
    public void displayCards(){
        this.send("Cards_" + PlayerHand.getString());
    }
    public void ready()
    {
    	this.ready = true;
    }
    public void notReady()
    {
    	this.ready = false;
    }
    public boolean getReady()
    {
    	return this.ready;
    }
    public void displayWinner(String winner)
    {
    	this.send("Winner_" + winner);
    }
    public void displayAmountOfExchanged(String exchangedCardCounter )
    {
    	this.send("exchangedCardCounter_" + exchangedCardCounter);
    }
    public void kill()
    {

    	connection.end();
    }
        public void run(String line){
                                //System.out.println("Player get line: " + line);
                                  //      this.send("startCoins_" + playersCoins);
                                    //    this.send("previousPlayers_" + game.introduceNewPlayer(name));
                    
                               System.out.println(line);
                                if (line.startsWith("Bet_")){
                                        game.updateBet(number,line.substring(4));
                                }
                                else if (line.startsWith("Check_")){
                                        game.nextPlayerMove();
                                }
                                else if (line.startsWith("All_")){
                                        game.updateAllin(number);
                                }
                                else if (line.startsWith("Fold_")){
                                        game.updateFold(number);
                                }
                                else if (line.startsWith("Exchange_")){
                                        game.exchangeCards(number,line.substring(9));
                                }
                                
                        
                        }

}
